package com.qiyadeng.wechat.baidu;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiyadeng.wechat.config.WeChatConstant;

public class BaiduMapService {
	Logger logger = LoggerFactory.getLogger(BaiduMapService.class);
	
	public String getPalace(String query,String lat,String lng) throws ClientProtocolException, IOException{
		HttpClient httpClient = new DefaultHttpClient();
		String url = palceRequestUrl(query,lat,lng);
		logger.debug(url);
		HttpGet httpget = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = httpClient.execute(httpget, responseHandler);
		logger.debug("baidu response:"+responseBody);
		return responseBody;
	}
	
	public String palceRequestUrl(String query,String lat,String lng) throws UnsupportedEncodingException {
		String url = WeChatConstant.BASEURL + "place/search?query=" + URLEncoder.encode(query,"UTF-8") + "&key="
				+ WeChatConstant.MAPKEY +"&location="+lat+","+lng +"&radius=2000"+"&output=" + WeChatConstant.OUTPUTFORMAT;
		return url;
	}
	
	public String getGeoCode(String query) throws ClientProtocolException, IOException{
		HttpClient httpClient = new DefaultHttpClient();
		String url = geoCodeRequestUrl(query);
		logger.debug(url);
		HttpGet httpget = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = httpClient.execute(httpget, responseHandler);
		logger.debug("baidu response:"+responseBody);
		return responseBody;
	}
	
	public String geoCodeRequestUrl(String query) throws UnsupportedEncodingException{
		String url = WeChatConstant.BASEURL + "geocoder?address=" + URLEncoder.encode(query,"UTF-8") + "&key="
				+ WeChatConstant.MAPKEY + "&output=" + WeChatConstant.OUTPUTFORMAT;
		return url;
	}
}

